<?php
namespace libs;

use Exception;
use components\O;
use caches\BaseCache;
//并发处理
class Concurrency extends BaseCache
{	
	const CONCURRENCY = 'concurrency_%s';
	const CACHE_LEAST_TIME = 1500;//1500ms 1.5s
	/**
	处理模块
	*/
	public static function handle($tag)
	{
		$tag = self::convertParamsType($tag);
		$key = self::gen(self::CONCURRENCY, json_encode($tag));
		while (self::exists($key)) {
			usleep(200000);//200ms
		}
		self::psetex($key,self::CACHE_LEAST_TIME,1);
	}
	/**
	tag数据类型转换
	*/
	public static function convertParamsType($tag)
	{
		if (is_numeric($tag)) {
			return strval($tag);
		} elseif (is_array($tag)) {
			foreach ($tag as $k => $v) {
				unset($tag[$k]);
				$tag[strval($k)] = self::convertParamsType($v);
			}
			return $tag;
		}
		throw new \Exception('params type not support:'.json_encode($tag));
	}
}